<?php
/**
 * @file newexclusivecontroller.php
 * @author LinLin
 * @date 2016年5月30日 上午11:49:53
 * @brief 
 */
namespace haibao\cms\business\control\pc\index;

use haibao\cms\business\control\PublicController;
use haibao\cms\business\control\pc\entity\ArticleEntity;
use haibao\cms\business\control\pc\action\ArticleAction;
use haibao\cms\business\control\PublicController as PublicControllerBusiness;

class NewExclusiveController extends IndexController{
    
    const TRENDY_TAG = 12204;
    const STAR_TAG = 1383;
    const JEWELRYWATCH_TAG = 6863;
    const FASHION_TAG = 310;
    const BEAUTY_TAG = 24;

    public function __construct($updateTime = null,$controlId,$isPublish){
        
        $this->conditionTagId = self::TRENDY_TAG;
        parent::__construct($updateTime,$controlId,$isPublish);
    }
    
    /**
     * 显示模版内容（模版动态数据的绑定和模版输出）
     * @param : String $content
     * @param : Array $controlData;
     */
    public function displayTemplateHtml(){
    
        $controlData = $this->getParameter(self::$CONTROL_DATA);
        $content = $this->getParameter(self::$CONTROL_CONTENT);
        $path = $this->generateHtmlPath(__CLASS__);
        $url = $title = $href = '';
        
        $exclusiveSerialName = array(
            '1383'=>array(
                22=>array('剧好看','/tvplay-list/'),
                31=>array('红毯点播台','/redcarpetradio-list/')
            ),
            '310'=>array(
                21=>array('摩豆','/modou-list/'),
                12204=>array('报君潮流眼','/fashion/12204/')
            ),
            '6863'=>array(
                29=>array('明星珠宝个人Show','/jewelshow-list/'),
                20=>array('每周必Buy','/weekbuy-list/')
            ),
        );
        
        $exclusiveSerial = array(
            //stars
            '1383'=>array(
                'exclusive'=>array(22,40,39,42),//剧好看   潮我看齐  报尚名来  摩范先生
            ),
            //fashion
            '310'=>array(
                'exclusive'=>array(52,36,43,21),//时装周  搭配速成班  恋物趣  摩豆
            ),
            //beauty
            '24'=>array(
                'exclusive'=>array(46,45,44,37,38),// 星妆上报   报告老师   街拍时妆  海报口碑测评 新品见报
            ),
            //jewelrywatch
            '6863'=>array(
                'exclusive'=>array(29,20),//明星珠宝个人秀  每周必Buy
            )
        );
        
        $littleExclusive = $exclusiveName =  $time = $exclusive = array();
        $channel = $controlData['manual']['topTagId'];
        //样式B 独家策划上侧列表 只有‘更多’，无其他专题
        if($controlData['manual']['columnId'] == 1){
            foreach($exclusiveSerial[$channel]['exclusive'] as $key=>$val){
                $temp = $this->getExclusive($channel,$val);
                if($temp && isset($temp[0])){
                    $exclusive[$val] = $temp[0];
                }
            }
            $this->assign('rightExclusiveName', $exclusiveSerialName[$channel]);
        }else{
         //样式A 独家策划上侧列表 显示该频道下专题标签
            foreach($exclusiveSerial[$channel]['exclusive'] as $key=>$val){
                $littleExclusive[$val] = $this->getExclusive($channel,$val);
            }
            //取报君潮流眼文章
            if($channel == self::FASHION_TAG){
                array_push($littleExclusive,$this->getFirstActualData(3,12204));
            }elseif ($channel == self::BEAUTY_TAG){
                array_push($littleExclusive,$this->getFirstActualData(3,13245));
            }

            if($littleExclusive){
                foreach($littleExclusive as $key=>$val){
                    foreach($val as $kk=>$vv){
                        if(isset($vv['exclusiveUrl'])){
                            $exclusiveName['name'] = $vv['exclusiveName'];
                            $exclusiveName['url'] = $vv['exclusiveUrl'];
                            $ee[$vv['exclusiveId']] = $exclusiveName;
                        }
                    }
                }
                if($channel == self::FASHION_TAG){
                    $ee[] = array('name'=>'报君潮流眼','url'=>'http://fashion.haibao.com/fashion/12204/');
                } elseif ($channel == self::BEAUTY_TAG) {
                    $ee[] = array('name'=>'美妆Live','url'=>'http://beauty.haibao.com/beauty/13245/');
                }
            }
            $littleExclusive = array_filter($littleExclusive);
            $littleChangeExclusive  = array();
            if($littleExclusive){
                foreach($littleExclusive as $key=>$val){
                    $littleChangeExclusive[] = $val;
                }
            }
            foreach($littleChangeExclusive as $key=>$val){
                foreach($val as $k=>$v){
                    if($k == 0){
                        $exclusive[] = $v;
                    }
                }
            }
            $this->assign('exclusiveName', $ee);
            $this->assign('littleExclusive', $littleChangeExclusive);
            
        }
        $order = array();
        foreach ($exclusive as $info) {
            $order[] = $info['PublishRiqi'];
        }
        array_multisort($order, SORT_DESC, $exclusive);
        $this->assign('exclusive', array_slice($exclusive, 0,3));
       
        $this->assign('channel', $channel);
        $this->assign('exclusiveUrl', \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_EXCLUSIVE));
        $this->assign('wwwUrl', \haibao\classlibrary\www\Config::getConfig(\haibao\classlibrary\www\Config::CLASSLIBRARY_CONFIG_HAIBAO_URL));
        $isNewHtml = $this->generateHtml($path,$content);
    
        $content = $this->getHtml($path);
        // 解析内容 更换链接
        $content = $this->replaceCDNLink($content);
        return $content;
    }
    
    private function getExclusive($channel,$serialIds,$columnIds = null){
        $exclusive = $columList = $column = $featureList = $tagIds = array();
        if($channel == self::JEWELRYWATCH_TAG){
            $featureList = \haibao\cms\business\control\FeatureController::getNewFeatureByChannelForIndex(0,1,false,1,$serialIds);
        }else{
            $featureList = \haibao\cms\business\control\FeatureController::getNewFeatureByChannelForIndex(0,3,false,1,$serialIds);
        }
        if($featureList){
            foreach($featureList as $key=>$val){
                $exclu['Id'] = $val->Id;
                $exclu['FeatureName'] = $val->FeatureDefine->FeatureName;
                $exclu['Url'] = $val->Url;
                $exclu['exclusiveId'] = $val->FeatureDefine->Serial->Id;
                $exclu['exclusiveName'] = $val->FeatureDefine->Serial->SerialName;
                $exclu['exclusiveUrl'] = \haibao\classlibrary\Config::getConfig(\haibao\classlibrary\Config::CLASSLIBRARY_CONFIG_HOST_EXCLUSIVE).'/featureflow/'.$serialIds.'/';
                $exclu['PublishRiqi']=  $val->ListTime->format('Y-m-d H:i:s');
                $exclu['ImageUrl'] = $val->Image->Filename;//$this->getImageLink($val->Image->Filename,300,225,100,0);
                $exclusive[] = $exclu;
            }
        }
        return $exclusive;
    }
    
    /**
     * 获取报君潮流眼数据
     * @param : $dataCondition 数据条件
     */
    public function getFirstActualData($limit,$tagId = 0){
        $articleEntity = new ArticleEntity();
        $articleEntity->setLevel(3);
        $articleEntity->setBigLevel(6);
        $articleEntity->setLimit($limit);
        $articleEntity->setIsProImage(true);
        $articleEntity->setIsLeftJoinTag(true);
        $articleEntity->setIsPrimaryTag(false);
        $articleEntity->setIsConditionTag(true);
        $articleEntity->setIsReturnTag(true);
        $articleEntity->setIsImageColl(false);
        $articleEntity->setSource(__CLASS__);
        $tagName = '报君潮流';
        if($tagId){
            $articleEntity->setTagIds($tagId);
            if($tagId == 13245){
                $tagName = '美妆Live';
            }
        } else {
            $articleEntity->setTagIds($this->conditionTagId);
        }

        $articleEntity->setIsDebug(false);
        $articleEntity->setIsOnlyReturnPrimaryTag(true);
        $articleEntity->setProImageLevel(\haibao\cms\model\data\ArticleProImage::Level_FIVE);
        $this->articleAction->setArticleEnity($articleEntity);
        list($articles,$articleProImages,$tagInfo,$articleTagArr) = $this->articleAction->getListForNew();
        $articleList = array();
        $articleLists = array();
        foreach($articles as $key=>$article){
            $articleList['Id'] = $article->Id;
            $articleList['FeatureName'] = isset($article->ShortTitle) ? $article->ShortTitle : $article->Title;
            $articleList['Url'] = self::getArticleUrl($article->PublishId,$article->ArticleTag->ParentId);
            $articleList['exclusiveName'] = isset($tagInfo[$article->ArticleTag->TagId]) ? $tagInfo[$article->ArticleTag->TagId]['TagName'] : $tagName;
            $articleList['PublishRiqi'] = $article->PublishTime->format('Y-m-d H:i:s');
            if(isset($articleProImages[$article->Id][\haibao\cms\model\data\ArticleProImage::Level_FIVE])){
                $articleList['ImageUrl'] = $articleProImages[$article->Id][\haibao\cms\model\data\ArticleProImage::Level_FIVE];
            }else{
                if(isset($articleProImages[$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO])){
                    $articleList['ImageUrl'] = $articleProImages[$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO];
                } else {
                    $articleList['ImageUrl'] = \haibao\cms\business\control\PublicController::getArticleFirstImage($article->Id);
                }
            }
            $articleList['SiteType'] = 1;
            array_push($articleLists,$articleList);
        }
        return $articleLists;
    }
    
} 